﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using Acms.AcmsControls;

public partial class Templates_Standard_flashxml : Acms.AcmsControls.TemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xml.WriteStartDocument();

            xml.WriteStartElement("ITEM");

            WriteStripImage(xml);

            xml.WriteStartElement("VIDEOS");
            for (int i = 1; i <= 3; i++)
            {
                WriteVideo(xml, i);
            }
            xml.WriteEndElement();//"VIDEOS"

            xml.WriteEndElement();//"ITEM"
            xml.Close();
            Response.Flush();

            Response.End();
        }
    }

    private void WriteVideo(XmlTextWriter xml, int i)
    {
        var videoControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0}", i));
        var xControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0} X", i));
        var yControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0} Y", i));
        if (videoControl != null)
        {
            var videoProperties = videoControl.GetProperties<Acms.AcmsControls.FlashVideoContentControlProperties>();
            if (videoProperties != null && videoProperties.HasResource())
            {
                int x = 50;
                if (xControl != null)
                {
                    var xProperties = xControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                    if (xProperties != null)
                    {
                        try
                        {
                            x = int.Parse(xProperties.Text.Trim());
                        }
                        catch (FormatException)
                        {
                        }
                    }
                }
                int y = 50;
                if (yControl != null)
                {
                    var yProperties = yControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                    if (yProperties != null)
                    {
                        try
                        {
                            y = int.Parse(yProperties.Text.Trim());
                        }
                        catch (FormatException)
                        {
                        }
                    }
                }

                xml.WriteStartElement("VIDEO");

                //xml.WriteAttributeString("itemURL", AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(videoProperties.ResourceId, this), this));
                xml.WriteAttributeString("itemURL", AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(videoProperties.ResourceId, this), this));
                xml.WriteAttributeString("posX", x.ToString());
                xml.WriteAttributeString("posY", y.ToString());

                xml.WriteEndElement();//VIDEO
            }
        }
    }

    private void WriteStripImage(XmlTextWriter xml)
    {
        var stripImageControl = AcmsContext.CurrentPost.FindPostControl("Strip Image");
        if (stripImageControl != null)
        {
            var stripImageProperties = stripImageControl.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
            if (stripImageProperties != null && stripImageProperties.HasResource())
            {
                xml.WriteStartElement("IMAGE");
                xml.WriteAttributeString("itemURL", AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(stripImageProperties.ResourceId, this), this));
                xml.WriteEndElement();//IMAGE
            }
        }
    }
}
